package com.dinggou.controller;

import com.dinggou.common.ResponseCode;
import com.dinggou.common.ServerResponse;
import com.dinggou.entity.Classse;
import com.dinggou.service.ClassseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Controller
@RequestMapping("/class")
public class ClassseController {

    @Resource(name="classseServiceImpl")  //这个注解表示自动注入adminSeerviceImpl业务实现类
    @Autowired      //该注解表示后紧跟的变量进行封装，即自动产生getter和setter
    private ClassseService classseService;


    @RequestMapping("/delete")
    @ResponseBody
    public ServerResponse deleteByPrimaryKey(Integer id){
        if(classseService.deleteByPrimaryKey(id)>0){
            return ServerResponse.createBySuccess(1,"删除管部门单位成功");
        }else{
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"删除数据失败");
        }
    }

    @RequestMapping("/insert")
    @ResponseBody
    public ServerResponse insert(Classse record){

        Classse classse=new Classse();
        classse.setName(record.getName());
        classse.setMajorId(record.getMajorId());
        classse.setMajorName(record.getMajorName());
        classse.setStudentNumber(record.getStudentNumber());

        if(classseService.insert(classse)>0){
            return ServerResponse.createBySuccess("添加数据成功",classse);
        }else{
            return ServerResponse.createByError();
        }
    }


    @RequestMapping("/insertSelective")
    @ResponseBody
    public ServerResponse insertSelective(Classse record){

        Classse classse=new Classse();
        classse.setName(record.getName());
        classse.setMajorId(record.getMajorId());
        classse.setMajorName(record.getMajorName());
        classse.setStudentNumber(record.getStudentNumber());

        if(classseService.insert(classse)>0){
            return ServerResponse.createBySuccess("添加数据成功",classse);
        }else{
            return ServerResponse.createByError();
        }
    }

    @RequestMapping("/selectByPrimaryKey")
    @ResponseBody
    public ServerResponse selectByPrimaryKey(Integer id,HttpServletRequest request, HttpServletResponse response) throws IOException {

        Classse classse=classseService.selectByPrimaryKey(id);

        if(classse != null){
            return ServerResponse.createBySuccess(1,classse);
        }else{
            return ServerResponse.createByErrorMessage("找不到管理员");
        }
    }


    @RequestMapping("/listall")
    @ResponseBody
    public ServerResponse listall(HttpServletRequest request, HttpServletResponse response){
        List<Classse> classseList=classseService.selectAll();
        if(classseList.size()>0){
            return ServerResponse.createBySuccess(classseList.size(),classseList);
        }else{
            return ServerResponse.createByErrorMessage("找不到管理员信息");
        }
    }


    @RequestMapping("/updateByPrimaryKeySelective")
    @ResponseBody
    public ServerResponse updateByPrimaryKeySelective(Classse record){
        Classse classse=new Classse();
        classse.setId(record.getId());
        classse.setName(record.getName());
        classse.setMajorId(record.getMajorId());
        classse.setMajorName(record.getMajorName());
        classse.setStudentNumber(record.getStudentNumber());

        if(classseService.updateByPrimaryKeySelective(classse)>0){
            return ServerResponse.createBySuccess("更新管理数据成功",classse);
        }else{
            return ServerResponse.createByErrorMessage("更新管理数据失败");
        }

    }

    @RequestMapping("/updateByPrimaryKey")
    @ResponseBody
    public ServerResponse updateByPrimaryKey(Classse record){
        Classse classse=new Classse();
        classse.setId(record.getId());
        classse.setName(record.getName());
        classse.setMajorId(record.getMajorId());
        classse.setMajorName(record.getMajorName());
        classse.setStudentNumber(record.getStudentNumber());

        if(classseService.updateByPrimaryKeySelective(classse)>0){
            return ServerResponse.createBySuccess("更新管理数据成功",classse);
        }else{
            return ServerResponse.createByErrorMessage("更新管理数据失败");
        }

    }
}